package com.ts.api.module.api.convert.plan.http;

import com.gitee.apanlh.exp.ParamParserException;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.common.redis.ApiMatchRedisKey;
import com.ts.api.module.api.convert.core.handler.ApiConvertHandler;
import com.ts.api.module.api.convert.core.http.client.ApiHttpMessage;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskApply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;
import com.ts.api.module.api.convert.platform.entity.RespPlatformMultiPhoneMaskDataExchange;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMaskApply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5Apply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5DataExchange;
import com.ts.api.module.api.convert.platform.handler.ApiPlatformPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.platform.handler.ApiPlatformPhoneMd5ConvertHandler;
import com.ts.api.module.product.entity.po.ProductPO;
import com.ts.api.module.product.entity.po.ProductPlanPO;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 *  业务响应字段处理
 *
 *  @author Pan
 */
@Component
@Deprecated
public class ApiPlanResponseHandler {

    /**
     *  响应撞库处理
     *
     *  @param  httpMessage         HTTP消息类
     *  @param  apiConvertHandler   API转换器
     *  @param  productPO           主体实体
     *  @param  productPlanPO       主体计划实体
     *  @return ApiHttpMessage
     */
    @Deprecated
    public ApiHttpMessage respDataExchangeHandle(ApiHttpMessage httpMessage, ApiConvertHandler apiConvertHandler, ProductPO productPO, ProductPlanPO productPlanPO) throws Exception {
        String responseBody = httpMessage.getResponseBody();

        //  机构响应-手机号md5模式响应
        if (apiConvertHandler instanceof ApiOrgPhoneMd5ConvertHandler apiOrgPhoneMd5ConvertHandler) {
            RespOrgPhoneMd5DataExchange respOrgPhoneMd5DataExchange = apiOrgPhoneMd5ConvertHandler.respDataExchange(responseBody);
            if (ValidParam.isNull(respOrgPhoneMd5DataExchange)) {
                throw new ParamParserException("撞库参数异常:API机构机构-md5模式未返回[RespOrgPhoneMd5DataExchange]请检查");
            }

            httpMessage.setHasSuc(respOrgPhoneMd5DataExchange.getHasBizSuc());
            httpMessage.setResponseBody(responseBody);
            return httpMessage;
        }

        //  机构响应-手机号掩码模式
        if (apiConvertHandler instanceof ApiOrgPhoneMaskConvertHandler apiOrgPhoneMaskConvertHandler) {
            RespOrgPhoneMaskDataExchange respOrgPhoneMaskDataExchange = apiOrgPhoneMaskConvertHandler.respDataExchange(responseBody);
            if (ValidParam.isNull(respOrgPhoneMaskDataExchange)) {
                throw new ParamParserException("撞库参数异常:API机构-掩码模式未返回[RespOrgPhoneMaskDataExchange]请检查");
            }

            //  设置本次业务状态
            httpMessage.setHasSuc(respOrgPhoneMaskDataExchange.getHasBizSuc());

            if (!httpMessage.hasSuc()) {
                httpMessage.setResponseBody(responseBody);
                return httpMessage;
            }

            httpMessage.setResponseBody(JsonUtils.toJson(respOrgPhoneMaskDataExchange));
            return httpMessage;
        }

        //  平台响应-手机号md5模式
        if (apiConvertHandler instanceof ApiPlatformPhoneMd5ConvertHandler apiPlatformPhoneMd5ConvertHandler) {
            RespPlatformPhoneMd5DataExchange respPlatformPhoneMd5DataExchange = apiPlatformPhoneMd5ConvertHandler.respDataExchange(responseBody);
            if (ValidParam.isNull(respPlatformPhoneMd5DataExchange)) {
                throw new ParamParserException("撞库参数异常:平台-手机号md5模式未返回[RespPlatFormDataExchangePhoneMd5]请检查");
            }

            //  设置本次业务状态
            httpMessage.setHasSuc(respPlatformPhoneMd5DataExchange.getHasBizSuc());

            if (!httpMessage.hasSuc()) {
                httpMessage.setResponseBody(responseBody);
                return httpMessage;
            }

            String convertStandardJson = JsonUtils.toJson(respPlatformPhoneMd5DataExchange);
            httpMessage.setResponseBody(convertStandardJson);

            //  数据对象
            String saveApplyId = respPlatformPhoneMd5DataExchange.getApplyId();
            if (ValidParam.isNotEmpty(saveApplyId)) {
                String phoneMd5 = httpMessage.getPhoneMd5();
                Map<String, String> saveMap = MapUtils.newLinkedHashMap(newMap -> {
                    newMap.put("phoneMd5", phoneMd5);
                    newMap.put("applyId", saveApplyId);
                });

                //  根据phoneMd5获取数据对象(我方接入下游-进件时根据phoneMd5获取本次applyId)
                String keyByPhoneMd5 = ApiMatchRedisKey.getDataExchangeUniqueIdByPhoneMd5(productPO.getId(), productPlanPO.getId(), phoneMd5);
                String saveValue = JsonUtils.toJson(saveMap);
                ApiMatchRedisKey.DOWNSTREAM_EXTEND.setForMinutes(keyByPhoneMd5, saveValue, 10);

                //  根据传递的applyId获取数据对象（我方接入下游-请求进件申请时根据applyId获取本次phoneMd5）
                String keyBySaveApplyId = ApiMatchRedisKey.getDataExchangeUniqueIdByApplyId(productPO.getId(), productPlanPO.getId(), saveApplyId);
                ApiMatchRedisKey.DOWNSTREAM_EXTEND.setForMinutes(keyBySaveApplyId, saveValue, 10);
            }
            return httpMessage;
        }

        //  平台响应-手机号掩码
        if (apiConvertHandler instanceof ApiPlatformPhoneMaskConvertHandler apiPlatformPhoneMaskConvertHandler) {
            RespPlatformMultiPhoneMaskDataExchange respPlatformMultiPhoneMaskDataExchange = apiPlatformPhoneMaskConvertHandler.respDataExchange(responseBody);
            if (ValidParam.isNull(respPlatformMultiPhoneMaskDataExchange)) {
                throw new ParamParserException("撞库参数异常:平台-手机号掩码模式未返回[RespPlatFormDataExchangeMultiPhoneMask]请检查");
            }

            //  设置本次业务状态
            httpMessage.setHasSuc(respPlatformMultiPhoneMaskDataExchange.getHasBizSuc());

            if (!httpMessage.hasSuc()) {
                httpMessage.setResponseBody(responseBody);
                return httpMessage;
            }

            //  如果平台响应成功则替换标准数据实体
            httpMessage.setResponseBody(JsonUtils.toJson(respPlatformMultiPhoneMaskDataExchange));
            return httpMessage;
        }
        throw new ParamParserException("未知的撞库响应转换器");
    }

    /**
     *  响应进件处理
     *
     *  @param  httpMessage         HTTP消息类
     *  @param  apiConvertHandler   API转换器
     *  @param  productPO           主体实体
     *  @param  productPlanPO       主体计划实体
     *  @return ApiHttpMessage
     *  @throws Exception
     */
    @Deprecated
    public ApiHttpMessage respApplyHandle(ApiHttpMessage httpMessage, ApiConvertHandler apiConvertHandler, ProductPO productPO, ProductPlanPO productPlanPO) throws Exception {
        String responseBody = httpMessage.getResponseBody();

        //  机构响应-手机号md5模式响应
        if (apiConvertHandler instanceof ApiOrgPhoneMd5ConvertHandler apiOrgPhoneMd5ConvertHandler) {
            RespOrgPhoneMd5Apply resp = apiOrgPhoneMd5ConvertHandler.respApply(responseBody);
            if (ValidParam.isNull(resp)) {
                throw new ParamParserException("进件参数异常:API机构机构-md5模式未返回[RespOrgPhoneMd5Apply]请检查");
            }

            httpMessage.setHasSuc(resp.getHasBizSuc());
            httpMessage.setResponseBody(responseBody);
            return httpMessage;
        }

        //  机构响应-手机号掩码模式
        if (apiConvertHandler instanceof ApiOrgPhoneMaskConvertHandler apiOrgPhoneMaskConvertHandler) {
            RespOrgPhoneMaskApply resp = apiOrgPhoneMaskConvertHandler.respApply(responseBody);
            if (ValidParam.isNull(resp)) {
                throw new ParamParserException("进件参数异常:API机构-掩码模式未返回[RespOrgPhoneMd5Apply]请检查");
            }

            httpMessage.setHasSuc(resp.getHasBizSuc());
            httpMessage.setResponseBody(responseBody);
            return httpMessage;
        }

        //  平台响应-手机号md5模式
        if (apiConvertHandler instanceof ApiPlatformPhoneMd5ConvertHandler apiPlatformPhoneMd5ConvertHandler) {
            RespPlatformPhoneMd5Apply resp = apiPlatformPhoneMd5ConvertHandler.respApply(responseBody);
            if (ValidParam.isNull(resp)) {
                throw new ParamParserException("进件参数异常:平台-手机号md5模式未返回[RespPlatFormPhoneMd5Apply]请检查");
            }

            httpMessage.setHasSuc(resp.getHasBizSuc());
            httpMessage.setResponseBody(responseBody);
            return httpMessage;
        }

        //  平台响应-手机号掩码
        if (apiConvertHandler instanceof ApiPlatformPhoneMaskConvertHandler apiPlatformPhoneMaskConvertHandler) {
            RespPlatformPhoneMaskApply resp = apiPlatformPhoneMaskConvertHandler.respApply(responseBody);
            if (ValidParam.isNull(resp)) {
                throw new ParamParserException("进件参数异常:平台-手机号掩码模式未返回[RespPlatFormPhoneMaskApply]请检查");
            }

            httpMessage.setHasSuc(resp.getHasBizSuc());
            httpMessage.setResponseBody(responseBody);
            return httpMessage;
        }
        throw new ParamParserException("未知的进件响应转换器");
    }
}
